﻿using System;
using System.Collections.Generic;
using System.Linq;
using Volo.Abp;

namespace Yyd.OrganizationUnits
{
    /// <summary>
    /// 
    /// </summary>
    public class OrganizationUnitLevel
    {
        public List<IOrganizationUnit> OrganizationUnits { get; }

        public OrganizationUnitLevel(List<IOrganizationUnit> organizationUnits)
        {
            Check.NotNullOrEmpty(organizationUnits, nameof(organizationUnits));

            OrganizationUnits = new List<IOrganizationUnit>();

            var rootUo = organizationUnits.SingleOrDefault(uo => uo.ParentId == null);

            if (rootUo == null)
            {
                throw new UserFriendlyException("必须有一个根组织单位！");
            }

            OrganizationUnits.Add(rootUo);

            AddItem(organizationUnits, rootUo.Id);
        }

        public OrganizationUnitLevel(IOrganizationUnit root) : this(new List<IOrganizationUnit> {root})
        {

        }

        /// <summary>
        /// 所在组织单位 根级节点
        /// </summary>
        public IOrganizationUnit Root => OrganizationUnits.FirstOrDefault();

        /// <summary>
        /// 所在组织单位 当前节点
        /// </summary>
        public IOrganizationUnit Current => OrganizationUnits.LastOrDefault();

        /// <summary>
        /// 获取层级中的部门
        /// </summary>
        /// <returns></returns>
        public IOrganizationUnit GetDepartment()
        {
            return OrganizationUnits.LastOrDefault(ou => ou.Type == OrganizationUnitType.Department);
        }


        /// <summary>
        /// 获取层级中的第一个门店 
        /// </summary>
        /// <returns></returns>
        public IOrganizationUnit GetSchool()
        {
            var school = OrganizationUnits.LastOrDefault(ou => ou.Type == OrganizationUnitType.School);
            if (school!=null)
            {
                return school;
            }else 
            {
                var area = GetArea();
                if (area!=null)
                {
                    return area;
                }
                else
                {
                    return GetCompany();
                }
            }
            
        }

        /// <summary>
        /// 获取层级中的第一个商户（区域），如果没有则返回 Null
        /// </summary>
        /// <returns></returns>
        public IOrganizationUnit GetArea()
        {
            return OrganizationUnits.LastOrDefault(ou => ou.Type == OrganizationUnitType.Area);
        }

        /// <summary>
        /// 获取层级中的第一个公司（原全国），如果没有则返回 Null
        /// </summary>
        /// <returns></returns>
        public IOrganizationUnit GetCompany()
        {
            return OrganizationUnits.LastOrDefault(ou => ou.Type == OrganizationUnitType.Company);
        }

        protected void AddItem(List<IOrganizationUnit> organizationUnits, Guid parentId)
        {
            var organizationUnit = organizationUnits.SingleOrDefault(uo => uo.ParentId == parentId);

            if (organizationUnit != null)
            {
                OrganizationUnits.Add(organizationUnit);

                AddItem(organizationUnits, organizationUnit.Id);
            }
        }
    }
}
